(function ($) {
	var sourceData;
	var nurseid=''
	var unUploadFiles=[] //未上传的图片
	var keysArr = [{'key':'ThesisTitle','type':'input','text':'论文题目'},{'key':'ThesisName','type':'input','text':'期刊名称'},
		{'key':'ThesisSpec','type':'list','text':'是否本专科'},{'key':'ThesisLevel','type':'list','text':'期刊级别'},
		{'key':'ThesisType','type':'list','text':'论文类型'},{'key':'ThesisPage','type':'input','text':'页码'},
		{'key':'tableData','type':'auth','text':'作者'},{'key':'ThesisPublic','type':'date','text':'发表日期'}]
	
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//提交
	//论文题目^期刊名称^是否本专科^期刊级别^期刊文章^页码^作者类型「作者姓名「病区「序号^发表日期^id^状态
	function submitNewData(){
		blurAllInput()
		var status = this.getAttribute('data-id');
		var parr = ''
		for (var i = 0; i < keysArr.length;i++) {
			if (i > 0) {
				parr +='^'
			}
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var tip = '请选择'
			var str = ''
			if (type == 'auth') { //作者
				str = prepareAuthData()
				id = 'F'
			}else{
				var dom = $('#'+id)[0];
				if (type == "input") {
					str = dom.value
					tip = '请输入'
				}else if (type == 'date') {
					str = dom.innerText
				}else{
					str = dom.getAttribute('data-id')
				}
			}
			if (str == '' && oneKey['isMust']!='0') {
				mui.toast(tip+oneKey['text'])
				showEmptyAnimation(id)
				return;
			}
			parr +=str
		}
		parr += '^'
		if (sourceData) {
			parr += sourceData['rw'];
		}
		parr += '^'+status;
		
		//console.log(parr)
		var pdict = {'nurseid':nurseid,'parr':parr}
		var paramDict = {'method':'web.INMSMComm:SaveThesis','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('提交失败')
				return 
			}
			if (unUploadFiles.length==0 ) {
				plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = getUploadFileToServerPath('发表论文备案')
			var method = getSaveFileMethod('发表论文备案')
			var paramDict={'id':msg,'path':path,'imgArr':unUploadFiles,'method':method}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		//console.log(JSON.stringify(keysArr))
		for (var i = 0; i < keysArr.length;i++) {
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var valueStr = sourceData[id]==undefined?"":sourceData[id]
			if (type == 'auth') { //作者 F「张璐诗「29「0,S「冯亮「1「1,T「张雪琴「2「2
				var htmlDict = getKyNewAuthText(valueStr)
				$('#F')[0].innerHTML = htmlDict['F']
				$('#S')[0].innerHTML = htmlDict['S']
				$('#T')[0].innerHTML = htmlDict['T']
			}else{
				var dom = $('#'+id)[0];
				if (type == "input") {
					dom.value = valueStr
				}else if (type == 'date') {
					dom.innerText = valueStr
				}else if (type == 'list'){
					dom.innerText = sourceData[id+'Desc']
					dom.setAttribute('data-id',valueStr)
				}
			}
		}
		updateFileUI('ThesisFile','fileUL',sourceData,sourceData['rw'],getUploadFileToServerPath('发表论文备案'))
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		var perInfo = self.perInfo
		nurseid = perInfo.loginID
		$('#CreatorName')[0].innerText = perInfo.loginName
		$('#applyLoc')[0].innerText = perInfo.loginWardDesc
		if (sourceData) { //修改
			updateUI()
		}
		//统计信息的关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		//第几作者
		mui('body').on('tap','.authText',function(){
			blurAllInput()
			var dom = this
			setTimeout(function() {
				toChosePerson(dom.id,'多选') //防止切换页面时键盘未隐藏
			}, 100);
		})
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped);
		window.addEventListener('resize',windowResize)
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		//选择附件
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			unUploadFiles = afterSelectFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-btn',function(){
			var method = getDeleteFileMethod('发表论文备案')
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr!=undefined) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
})(mui)

function selectPerson(domId,params){
	var dom = mui('#'+domId)[0]
	//var id = ''
	var html=''
	for(var i = 0; i < params.length;i++){
		var onePer = params[i]
		html += '<div data-id="'+onePer['per']+'" class="oneAuth">'+onePer['perName']+'</div>'+
			'<div data-id="'+onePer['ward']+'" class="oneAuth">'+onePer['wardDesc']+'</div>'
	}
	
	if (html == '') {
		html = '<div class="authSelectTip">添加作者</div>'
	}
	dom.innerHTML = html;
}
